"""
Created on 21 mrt. 2012

@author: Daan
"""
import unittest
import os
import shutil

from unittests.stubs import builtin_open, stub_bases

from pCentralServer import CentralServer
from pCentralServer.plugins import Configuration

TEST_DIR = os.path.join(os.path.dirname(__file__), "dat")
UNITTEST_OUTPUT_DIR = os.path.join(TEST_DIR, "output")

#noinspection PyTypeChecker, PyCallByClass
class ConfigurationTester(unittest.TestCase):
    verbose = False

    def setUp(self):
        """
        DOCSTRING
        """

        if not os.path.isdir(UNITTEST_OUTPUT_DIR):
            os.makedirs(UNITTEST_OUTPUT_DIR)

    def tearDown(self):
        """
        DOCSTRING
        """
        shutil.rmtree(UNITTEST_OUTPUT_DIR, True)

    def test_01_instantiation(self):
        """
        DOCSTRING
        """
        with stub_bases.stub_bases(CentralServer.CentralServer, Configuration.Configuration):
            testObject = CentralServer.CentralServer(UNITTEST_OUTPUT_DIR)

            self.assertTrue(isinstance(testObject, CentralServer.CentralServer))
            self.assertTrue(isinstance(testObject, Configuration.Configuration))

    def test_04a_store_configruation(self):
        """
        DOCSTRING
        """

        with stub_bases.stub_bases(CentralServer.CentralServer, Configuration.Configuration):
            testObject = CentralServer.CentralServer(UNITTEST_OUTPUT_DIR)

            self.assertTrue(Configuration.Configuration.storeConfiguration(testObject, "myUnitTest1",
                    {"myVar1": 123, "myVar2": "abc"}))
            self.assertTrue(Configuration.Configuration.storeConfiguration(testObject, "myUnitTest2",
                    {"myVar1": 456, "myVar2": "def"}))

            self.assertTrue(os.path.exists(os.path.join(UNITTEST_OUTPUT_DIR, "myUnitTest1.config")))
            self.assertTrue(os.path.exists(os.path.join(UNITTEST_OUTPUT_DIR, "myUnitTest1.config")))

    def test_04b_store_configuration_fail(self):
        """
        DOCSTRING
        """

        with stub_bases.stub_bases(CentralServer.CentralServer, Configuration.Configuration):
            testObject = CentralServer.CentralServer(UNITTEST_OUTPUT_DIR)

            with builtin_open.builtin_open(verbose=self.verbose).open_fixed_ioerror(
                [os.path.join(UNITTEST_OUTPUT_DIR, "myUnitTest1.config")]):
                self.assertFalse(Configuration.Configuration.storeConfiguration(testObject, "myUnitTest1",
                        {"myVar1": 123, "myVar2": "abc"}))
                self.assertFalse(os.path.exists(os.path.join(UNITTEST_OUTPUT_DIR, "myUnitTest1.config")))

    def test_05a_retrieve_configruation(self):
        """
        DOCSTRING
        """

        with stub_bases.stub_bases(CentralServer.CentralServer, Configuration.Configuration):
            testObject = CentralServer.CentralServer(TEST_DIR)

            self.assertEqual(Configuration.Configuration.retrieveConfiguration(testObject, "myUnitTest1"),
                    {"myVar1": 123, "myVar2": "abc"})
            self.assertEqual(Configuration.Configuration.retrieveConfiguration(testObject, "myUnitTest2"),
                    {"myVar1": 456, "myVar2": "def"})

    def test_05b_retrieve_configuration_fail(self):
        """
        DOCSTRING
        """

        with stub_bases.stub_bases(CentralServer.CentralServer, Configuration.Configuration):
            testObject = CentralServer.CentralServer(UNITTEST_OUTPUT_DIR)

            with builtin_open.builtin_open(verbose=self.verbose).open_fixed_ioerror(
                [os.path.join(TEST_DIR, "myUnitTest1.config")]):
                self.assertEqual(Configuration.Configuration.retrieveConfiguration(testObject, "myUnitTest1"), None)

if __name__ == "__main__":
    unittest.main()